package com.bocai.kelala.ui.travel.main.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelala.App;
import com.bocai.kelala.R;
import com.bocai.kelala.base.BaseActivity;
import com.bocai.kelala.bean.MainListBean;
import com.bocai.kelala.bean.UserBean;
import com.bocai.kelala.ui.travel.main.ada.MainAdapter;
import com.bocai.kelala.ui.travel.main.contract.MainListContract;
import com.bocai.kelala.ui.travel.main.module.MainListModule;
import com.bocai.kelala.ui.travel.main.presenter.MainListPresenter;
import com.bocai.kelala.ui.travel.set.act.CertificationInformationActivity;
import com.bocai.kelala.ui.travel.set.act.CertificationInformationStateActivity;
import com.bocai.kelala.ui.travel.stroke.act.PublishStrokeActivity;
import com.bocai.kelala.ui.travel.stroke.act.PublishStrokeAddressActivity;
import com.bocai.kelala.util.SP;
import com.bocai.kelala.util.ToastUitl;
import com.bocai.mybase.base.BaseRecyclerAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dalvik.system.PathClassLoader;

/**
 * Created by yuanfei on 2017/6/30.
 */

public class MainListActivity extends BaseActivity implements MainListContract.view, XRecyclerView.LoadingListener {

    MainAdapter mainAdapter;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.ll_delete)
    LinearLayout llDelete;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_start_address)
    TextView tvStartAddress;
    @BindView(R.id.tv_end_address)
    TextView tvEndAddress;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.btn_cancel)
    AppCompatButton btnCancel;
    @BindView(R.id.xrecycle)
    XRecyclerView xrecycle;
    @BindView(R.id.main)
    LinearLayout main;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;
    @BindView(R.id.ll_back)
    LinearLayout llBack;

    private int pageIndex = 1;
    @Inject
    MainListPresenter presenter;
    private String time, seats, originCity, arriveCity, time1;
    private String originCityName, arriveCityName;

    @Override
    public int getLayoutId() {
        return R.layout.act_main_list;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new MainListModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("客拉拉");
        initBack(imgBack, llBack);
        time = getIntent().getStringExtra("time");
        time1 = getIntent().getStringExtra("time1");
        seats = getIntent().getStringExtra("seats");
        originCity = getIntent().getStringExtra("originCity");
        arriveCity = getIntent().getStringExtra("arriveCity");
        originCityName = getIntent().getStringExtra("originCityName");
        arriveCityName = getIntent().getStringExtra("arriveCityName");
        tvStartAddress.setText(originCityName.replace("市", ""));
        tvEndAddress.setText(arriveCityName.replace("市", ""));
        tvStartTime.setText(time);
        tvNum.setText("车的座位是" + seats + "座及以上");
        xrecycle.setLayoutManager(new LinearLayoutManager(this));
        mainAdapter = new MainAdapter();
        xrecycle.setAdapter(mainAdapter);
        xrecycle.setEmptyView(llEmpty);
        xrecycle.setLoadingListener(this);
        xrecycle.refresh();
        mainAdapter.setOnItemOnListener(new BaseRecyclerAdapter.OnItemClickedListener() {
            @Override
            public void onItemClicked(int id, int index) {
                if (TextUtils.isEmpty(SP.get(MainListActivity.this, "id", "").toString())) {
//                    setDialog();
//                    showErrorTip("请先登录");
                } else {
                    startActivityForResult(new Intent(MainListActivity.this, VehicleDetailActivity.class).putExtra("orderId", mainAdapter.getDataList().get(index).getOrderId()), 27);
                }
            }
        });
    }

    @OnClick({R.id.btn_cancel, R.id.ll_cancel, R.id.btn_publish})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.ll_cancel:
                finish();
                break;
            case R.id.btn_publish:
                if ((int) SP.get(this, "ApprovalStatus", 0) == 0) {
                    startActivity(new Intent(this, CertificationInformationActivity.class));
                } else if ((int) SP.get(this, "ApprovalStatus", 0) == -1) {
                    startActivity(new Intent(this, CertificationInformationStateActivity.class));
                } else if ((int) SP.get(this, "ApprovalStatus", 0) == 1) {
                    presenter.getUser();
                } else {
                    startActivity(PublishStrokeAddressActivity.class);
                }
                break;
        }
    }

    @Override
    public void getHomeListSuccess(MainListBean bean) {
        xrecycle.refreshComplete();
        xrecycle.loadMoreComplete();
        if (bean != null && bean.getOrderList() != null && bean.getOrderList().size() != 0) {
            if (pageIndex == 1) {
                mainAdapter.clear();
                mainAdapter.addItems(bean.getOrderList());
            } else {
                mainAdapter.addItems(bean.getOrderList());
            }
            if (bean.getOrderList().size() > 10) {
                xrecycle.setLoadingMoreEnabled(false);
            } else {
                xrecycle.setLoadingMoreEnabled(true);
            }
        } else {
            xrecycle.setLoadingMoreEnabled(false);

        }
    }

    @Override
    public void getUser(UserBean userBean) {
        if (userBean == null||userBean.getId() == null){

        }else {
            SP.putUser(MainListActivity.this, userBean);
            if ((int) SP.get(this, "ApprovalStatus", 0) == 0) {
                startActivity(new Intent(this, CertificationInformationActivity.class));
            } else if ((int) SP.get(this, "ApprovalStatus", 0) == -1) {
                startActivity(new Intent(this, CertificationInformationStateActivity.class));
            } else if ((int) SP.get(this, "ApprovalStatus", 0) == 1) {
                ToastUitl.showShort("审核中");
            }else {
                startActivity(PublishStrokeAddressActivity.class);
            }
        }
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        if (msg.equals("用户授权失败")) {
            msg = "您的登录已失效，请重新登录";
            startActivity(new Intent(this, MainActivity.class).putExtra("index", 1));
            finish();
        } else if (msg.equals("网络异常")) {
            xrecycle.refreshComplete();
            xrecycle.loadMoreComplete();
        }
        showShortToast(msg);
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        presenter.homeList(pageIndex + "", "10", time1, seats, originCityName, arriveCityName);
    }

    @Override
    public void onLoadMore() {
        pageIndex = pageIndex + 1;
        presenter.homeList(pageIndex + "", "10", time1, seats, originCityName, arriveCityName);
    }
}
